/*
 * AddRelationPanel.java
 *
 * Created on Sobota, 2008, január 12, 18:03
 */
package eu.easyedu.dictionary.gui;

import eu.easyedu.dictionary.api.model.Language;
import eu.easyedu.dictionary.api.model.Relationship;
import eu.easyedu.dictionary.api.model.Word;
import eu.easyedu.dictionary.api.model.WordType;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
 *
 * @author  hlavki
 */
public class WordPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(WordPanel.class.getName());
    private Word word;
    private Relationship relationship;


    /** Creates new form AddRelationPanel */
    public WordPanel() {
        this(new Word(), null);
    }


    public WordPanel(Word word) {
        this(word, null);
    }


    public WordPanel(Word word, Relationship relationship) {
        initComponents();
        setWord(word);
        this.relationship = relationship;
        setBorder();
        wordTextField.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent evt) {
                wordTextFieldDocumentPerformed(evt);
            }


            public void removeUpdate(DocumentEvent evt) {
                wordTextFieldDocumentPerformed(evt);
            }


            public void changedUpdate(DocumentEvent evt) {
                wordTextFieldDocumentPerformed(evt);
            }
        });
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        wordTextField = new javax.swing.JTextField();
        languageComboBox = new javax.swing.JComboBox();
        wordTypeComboBox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        notesTextArea = new javax.swing.JTextArea();

        wordTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wordTextFieldActionPerformed(evt);
            }
        });

        languageComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                languageComboBoxActionPerformed(evt);
            }
        });

        wordTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wordTypeComboBoxActionPerformed(evt);
            }
        });

        notesTextArea.setColumns(20);
        notesTextArea.setRows(2);
        notesTextArea.setTabSize(4);
        jScrollPane1.setViewportView(notesTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(wordTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(languageComboBox, 0, 103, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(wordTypeComboBox, 0, 106, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(wordTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(languageComboBox)
                    .addComponent(wordTypeComboBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void wordTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wordTextFieldActionPerformed
    }//GEN-LAST:event_wordTextFieldActionPerformed

    private void languageComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_languageComboBoxActionPerformed
        word.setLanguage((Language) languageComboBox.getSelectedItem());
    }//GEN-LAST:event_languageComboBoxActionPerformed

    private void wordTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wordTypeComboBoxActionPerformed
        word.setType((WordType) wordTypeComboBox.getSelectedItem());
    }//GEN-LAST:event_wordTypeComboBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox languageComboBox;
    private javax.swing.JTextArea notesTextArea;
    private javax.swing.JTextField wordTextField;
    private javax.swing.JComboBox wordTypeComboBox;
    // End of variables declaration//GEN-END:variables


    public ComboBoxModel getLanguageComboBoxModel() {
        return languageComboBox.getModel();
    }


    public ComboBoxModel getWordTypeComboBoxModel() {
        return wordTypeComboBox.getModel();
    }


    public void setLanguageComboBoxModel(ComboBoxModel languageComboBoxModel) {
        this.languageComboBox.setModel(languageComboBoxModel);
        reloadWordSettings();
    }


    public void setWordTypeComboBoxModel(ComboBoxModel wordTypeComboBoxModel) {
        this.wordTypeComboBox.setModel(wordTypeComboBoxModel);
        reloadWordSettings();
    }


    public JComboBox getTypeComboBox() {
        return wordTypeComboBox;
    }


    public JComboBox getLanguageComboBox() {
        return languageComboBox;
    }


    public JTextField getWordTextField() {
        return wordTextField;
    }


    public Word getWord() {
        word.setLanguage((Language) languageComboBox.getSelectedItem());
        word.setType((WordType) wordTypeComboBox.getSelectedItem());
        word.setNotes(notesTextArea.getText());
        return word;
    }


    public void setWord(Word word) {
        this.word = word;
        reloadWordSettings();
    }


    public void setRelationship(Relationship relationship) {
        this.relationship = relationship;
        setBorder();
    }


    protected void reloadWordSettings() {
        if (word.getWord() != null) {
            wordTextField.setText(this.word.getWord());
        }
        if (word.getLanguage() != null) {
            languageComboBox.setSelectedItem(word.getLanguage());
        }
        if (word.getType() != null) {
            wordTypeComboBox.setSelectedItem(word.getType());
        }
        if (word.getNotes() != null) {
            notesTextArea.setText(word.getNotes());
        }
        if (word.exists()) {
            wordTextField.setEnabled(false);
            languageComboBox.setEnabled(false);
            wordTypeComboBox.setEnabled(false);
            notesTextArea.setEnabled(false);
        }
    }


    public Relationship getRelationship() {
        return relationship;
    }


    public boolean isRelated() {
        return relationship != null;
    }


    public void addWordTypeComboBoxActionListener(ActionListener listener) {
        wordTypeComboBox.addActionListener(listener);
    }


    public void removeWordTypeComboBoxActionListener(ActionListener listener) {
        wordTextField.removeActionListener(listener);
    }


    public void setWordType(WordType type) {
        this.word.setType(type);
        reloadWordSettings();
    }


    private final void wordTextFieldDocumentPerformed(DocumentEvent evt) {
        Document doc = evt.getDocument();
        try {
            word.setWord(doc.getText(0, doc.getLength()));
        } catch (BadLocationException e) {
            log.severe(e.getMessage());
        }
    }


    private void setBorder() {
        if (isRelated()) {
            setBorder(new TitledBorder(new EtchedBorder(), relationship.toString()));
        }
    }
}
